package org.dawnsql.stock.util;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.internal.LinkedTreeMap;
import com.google.gson.reflect.TypeToken;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 找到买点
 * */
public class MaiDian {
    // jQuery351058620222703612_1749034047746({"rc":0,"rt":17,"svr":175642633,"lt":1,"full":0,"dlmkts":"","data":{"code":"688702","market":1,"name":"盛科通信-U","decimal":2,"dktotal":412,"preKPrice":69.37,"klines":["2024-12-03,70.38,69.97,71.21,68.60,18752,131219157.00,3.76,0.86,0.60,0.93","2024-12-04,75.52,71.36,80.12,71.27,37329,279252132.00,12.65,1.99,1.39,1.85","2024-12-05,71.00,71.80,74.65,71.00,20122,145577420.00,5.11,0.62,0.44,1.00","2024-12-06,71.50,72.48,74.98,71.38,25292,185053035.00,5.01,0.95,0.68,1.26","2024-12-09,72.13,72.02,73.33,70.36,18069,129552133.00,4.10,-0.63,-0.46,0.90","2024-12-10,75.93,73.76,77.90,73.03,27754,208287275.00,6.76,2.42,1.74,1.38","2024-12-11,72.42,73.65,74.00,71.12,23420,170645737.00,3.90,-0.15,-0.11,1.16","2024-12-12,73.44,72.62,74.20,71.80,30682,223513243.00,3.26,-1.40,-1.03,1.52","2024-12-13,72.55,72.38,76.90,72.38,65607,492363549.00,6.22,-0.33,-0.24,3.26","2024-12-16,78.98,78.35,81.66,74.00,66221,516181167.00,10.58,8.25,5.97,3.29","2024-12-17,76.41,75.11,82.66,74.73,38247,299629899.00,10.12,-4.14,-3.24,1.90","2024-12-18,73.51,81.45,83.32,72.62,40975,324103926.00,14.25,8.44,6.34,2.03","2024-12-19,79.00,90.00,97.68,79.00,50790,438543072.00,22.93,10.50,8.55,2.52","2024-12-20,87.58,93.53,97.33,87.20,37968,350266825.00,11.26,3.92,3.53,1.88","2024-12-23,87.45,88.00,89.30,81.81,60338,513539596.00,8.01,-5.91,-5.53,2.99","2024-12-24,86.90,86.17,87.79,83.07,37319,318849754.00,5.36,-2.08,-1.83,1.85","2024-12-25,86.30,90.58,92.17,85.12,28594,254301409.00,8.18,5.12,4.41,1.42","2024-12-26,88.85,95.49,96.90,88.23,31181,291476045.00,9.57,5.42,4.91,1.55","2024-12-27,94.80,89.00,95.01,88.37,29820,271239816.00,6.95,-6.80,-6.49,1.48","2024-12-30,88.90,87.66,89.50,86.31,21164,186381864.00,3.58,-1.51,-1.34,1.05","2024-12-31,87.37,84.00,88.50,83.53,26197,223148642.00,5.67,-4.18,-3.66,1.30","2025-01-02,84.00,78.76,84.00,77.75,26336,210987963.00,7.44,-6.24,-5.24,1.31","2025-01-03,77.63,75.49,79.54,74.74,25567,194971107.00,6.09,-4.15,-3.27,1.27","2025-01-06,74.70,71.77,77.22,71.16,27476,201625218.00,8.03,-4.93,-3.72,1.36","2025-01-07,72.01,77.86,78.66,71.05,30200,227866447.00,10.60,8.49,6.09,1.50","2025-01-08,76.70,77.99,79.48,75.69,26887,208145264.00,4.87,0.17,0.13,1.33","2025-01-09,77.00,78.00,80.80,77.00,23969,189448902.00,4.87,0.01,0.01,1.19","2025-01-10,78.00,78.97,85.00,77.05,30225,247406140.00,10.19,1.24,0.97,1.50","2025-01-13,76.89,79.79,80.40,76.00,19867,156038791.00,5.57,1.04,0.82,0.99","2025-01-14,80.43,83.83,84.98,78.57,30470,249146033.00,8.03,5.06,4.04,1.51","2025-01-15,83.48,80.39,84.21,80.10,26836,220639778.00,4.90,-4.10,-3.44,1.33","2025-01-16,80.50,77.59,81.40,75.81,38781,301928373.00,6.95,-3.48,-2.80,1.92","2025-01-17,77.50,84.29,84.80,76.47,42874,348144444.00,10.74,8.64,6.70,2.13","2025-01-20,85.00,83.93,85.00,80.31,23355,193802050.00,5.56,-0.43,-0.36,1.16","2025-01-21,84.30,89.59,91.66,83.00,30282,267485411.00,10.32,6.74,5.66,1.50","2025-01-22,89.59,92.69,95.56,88.50,24989,232133020.00,7.88,3.46,3.10,1.24","2025-01-23,93.52,90.40,96.52,90.09,22180,206032106.00,6.94,-2.47,-2.29,1.10","2025-01-24,89.71,86.04,92.43,85.30,37277,328047839.00,7.89,-4.82,-4.36,1.85","2025-01-27,87.56,82.60,87.56,82.52,18336,153828835.00,5.86,-4.00,-3.44,0.91","2025-02-05,83.90,83.64,86.18,81.55,23587,199114381.00,5.61,1.26,1.04,1.17","2025-02-06,83.64,85.60,86.86,82.36,32204,273967001.00,5.38,2.34,1.96,1.60","2025-02-07,85.96,83.10,86.67,81.50,40651,340750037.00,6.04,-2.92,-2.50,2.02","2025-02-10,83.10,82.92,86.65,80.11,37851,314053767.00,7.87,-0.22,-0.18,1.88","2025-02-11,83.00,81.54,83.05,80.30,27746,225965989.00,3.32,-1.66,-1.38,1.38","2025-02-12,81.52,82.16,83.23,80.67,19768,162284360.00,3.14,0.76,0.62,0.98","2025-02-13,82.60,77.32,82.78,77.29,31181,246725897.00,6.68,-5.89,-4.84,1.55","2025-02-14,76.65,74.42,77.97,73.25,42229,315244736.00,6.10,-3.75,-2.90,2.10","2025-02-17,75.22,76.98,78.38,74.42,38432,292754021.00,5.32,3.44,2.56,1.91","2025-02-18,77.01,72.36,77.24,72.00,31846,236115712.00,6.81,-6.00,-4.62,1.58","2025-02-19,73.00,72.99,74.40,71.11,36187,263878832.00,4.55,0.87,0.63,1.80","2025-02-20,72.99,72.75,74.20,71.79,29001,210551484.00,3.30,-0.33,-0.24,1.44","2025-02-21,75.00,78.76,80.89,72.90,69895,541963766.00,10.98,8.26,6.01,3.47","2025-02-24,78.50,76.20,79.48,75.00,38393,295331494.00,5.69,-3.25,-2.56,1.91","2025-02-25,74.80,78.87,80.50,73.98,39352,306294154.00,8.56,3.50,2.67,1.95","2025-02-26,78.55,78.80,80.50,77.01,31971,252162571.00,4.43,-0.09,-0.07,1.59","2025-02-27,78.08,79.20,82.42,77.50,37498,300006348.00,6.24,0.51,0.40,1.86","2025-02-28,78.18,71.29,78.79,70.50,41205,303904844.00,10.47,-9.99,-7.91,2.04","2025-03-03,71.30,73.53,74.74,69.18,40341,293893655.00,7.80,3.14,2.24,2.00","2025-03-04,72.68,73.88,75.40,72.06,27745,204774523.00,4.54,0.48,0.35,1.38","2025-03-05,73.92,72.57,74.11,71.59,24018,174369093.00,3.41,-1.77,-1.31,1.19","2025-03-06,72.50,76.21,77.77,72.38,34565,261839669.00,7.43,5.02,3.64,1.72","2025-03-07,75.90,74.56,77.50,73.80,23116,174339047.00,4.86,-2.17,-1.65,1.15","2025-03-10,74.10,73.58,74.55,72.03,19206,140466120.00,3.38,-1.31,-0.98,0.95","2025-03-11,72.19,71.40,73.52,70.40,27213,194083462.00,4.24,-2.96,-2.18,1.35","2025-03-12,71.57,70.75,73.60,69.80,28133,201160763.00,5.32,-0.91,-0.65,1.40","2025-03-13,70.75,68.55,70.76,67.77,23480,161845862.00,4.23,-3.11,-2.20,1.17","2025-03-14,68.37,69.55,70.38,68.00,20430,141362900.00,3.47,1.46,1.00,1.01","2025-03-17,70.00,68.67,70.20,67.88,18848,129141108.00,3.34,-1.27,-0.88,0.94","2025-03-18,67.97,68.68,70.18,67.97,15458,106374926.00,3.22,0.01,0.01,0.77","2025-03-19,68.08,66.76,68.93,66.18,19345,129860999.00,4.00,-2.80,-1.92,0.96","2025-03-20,66.47,66.28,67.49,65.74,14788,98407091.00,2.62,-0.72,-0.48,0.73","2025-03-21,65.90,63.88,66.46,63.80,29297,189947495.00,4.01,-3.62,-2.40,1.45","2025-03-24,64.00,64.62,66.47,63.72,20611,133594792.00,4.30,1.16,0.74,1.02","2025-03-25,65.01,64.16,66.58,63.97,19254,124881135.00,4.04,-0.71,-0.46,0.96","2025-03-26,64.20,64.45,65.34,63.90,10452,67473419.00,2.24,0.45,0.29,0.52","2025-03-27,64.00,64.03,65.33,62.96,17740,113607159.00,3.68,-0.65,-0.42,0.88","2025-03-28,64.10,62.80,64.65,62.50,11880,75240056.00,3.36,-1.92,-1.23,0.59","2025-03-31,61.82,64.67,65.78,61.70,25309,163060156.00,6.50,2.98,1.87,1.26","2025-04-01,64.48,64.22,66.15,63.91,19597,127717411.00,3.46,-0.70,-0.45,0.97","2025-04-02,64.12,63.80,65.38,63.20,13603,87163036.00,3.39,-0.65,-0.42,0.68","2025-04-03,63.32,63.33,65.19,62.96,11501,73348715.00,3.50,-0.74,-0.47,0.57","2025-04-07,57.99,56.46,61.50,54.50,31702,184840740.00,11.05,-10.85,-6.87,1.57","2025-04-08,58.78,59.80,60.19,57.00,34595,204777282.00,5.65,5.92,3.34,1.72","2025-04-09,59.10,62.76,64.99,58.66,34991,218431886.00,10.59,4.95,2.96,1.74","2025-04-10,64.39,63.58,64.85,62.75,21165,135216023.00,3.35,1.31,0.82,1.05","2025-04-11,64.40,69.06,71.90,63.48,48679,333062903.00,13.24,8.62,5.48,2.42","2025-04-14,69.09,69.41,71.33,67.74,37395,261601825.00,5.20,0.51,0.35,1.86","2025-04-15,68.93,67.90,69.86,66.62,28442,191776576.00,4.67,-2.18,-1.51,1.41","2025-04-16,67.98,68.19,69.45,67.90,19609,134265717.00,2.28,0.43,0.29,0.97","2025-04-17,67.77,69.66,70.09,67.77,27030,187870012.00,3.40,2.16,1.47,1.34","2025-04-18,69.60,65.61,69.60,65.01,22693,151016110.00,6.59,-5.81,-4.05,1.13","2025-04-21,65.00,64.68,66.66,64.43,20239,132114191.00,3.40,-1.42,-0.93,1.00","2025-04-22,64.60,64.25,65.48,63.00,13809,88906395.00,3.83,-0.66,-0.43,0.69","2025-04-23,65.16,65.10,65.77,63.85,15639,101275843.00,2.99,1.32,0.85,0.78","2025-04-24,65.06,62.94,65.10,62.30,15397,97268285.00,4.30,-3.32,-2.16,0.76","2025-04-25,62.75,62.90,63.80,62.11,13195,82989097.00,2.69,-0.06,-0.04,0.65","2025-04-28,61.64,62.86,64.11,60.69,15087,94021113.00,5.44,-0.06,-0.04,0.75","2025-04-29,62.80,63.66,63.95,62.13,9263,58629860.00,2.90,1.27,0.80,0.46","2025-04-30,63.66,66.69,67.61,63.16,24031,158511746.00,6.99,4.76,3.03,1.19","2025-05-06,66.94,66.60,67.73,65.50,17090,113721553.00,3.34,-0.13,-0.09,0.85","2025-05-07,66.99,64.35,68.58,63.83,17315,113335751.00,7.13,-3.38,-2.25,0.86","2025-05-08,64.36,64.88,66.12,63.87,13674,89154758.00,3.50,0.82,0.53,0.68","2025-05-09,64.86,62.15,64.96,61.89,17202,107897906.00,4.73,-4.21,-2.73,0.85","2025-05-12,62.88,61.83,63.50,60.38,18950,116336298.00,5.02,-0.51,-0.32,0.94","2025-05-13,62.67,60.99,63.00,60.85,13416,82616042.00,3.48,-1.36,-0.84,0.67","2025-05-14,61.16,60.69,61.66,60.06,14693,89162663.00,2.62,-0.49,-0.30,0.73","2025-05-15,60.81,59.09,60.81,59.09,10010,59573081.00,2.83,-2.64,-1.60,0.50","2025-05-16,59.99,59.89,60.22,58.22,16350,96545092.00,3.38,1.35,0.80,0.81","2025-05-19,61.47,60.08,61.64,59.28,12519,75257123.00,3.94,0.32,0.19,0.62","2025-05-20,60.37,59.99,60.70,58.90,11738,70447253.00,3.00,-0.15,-0.09,0.58","2025-05-21,60.14,58.50,60.20,58.04,15133,89437867.00,3.60,-2.48,-1.49,0.75","2025-05-22,58.25,57.96,59.25,57.88,11907,69648609.00,2.34,-0.92,-0.54,0.59","2025-05-23,57.99,58.03,58.59,57.00,13681,79224857.00,2.74,0.12,0.07,0.68","2025-05-26,58.78,58.13,59.28,56.68,11538,67424553.00,4.48,0.17,0.10,0.57","2025-05-27,58.13,57.99,58.99,57.23,10517,61011279.00,3.03,-0.24,-0.14,0.52","2025-05-28,57.99,57.70,58.86,57.23,9001,52059008.00,2.81,-0.50,-0.29,0.45","2025-05-29,57.69,59.20,59.20,57.10,10756,62881253.00,3.64,2.60,1.50,0.53","2025-05-30,58.98,59.45,59.50,57.80,9224,54145109.00,2.87,0.42,0.25,0.46","2025-06-03,59.45,61.58,62.65,58.50,25553,158151412.00,6.98,3.58,2.13,1.27","2025-06-04,62.21,63.52,63.84,61.58,21327,134540296.00,3.67,3.15,1.94,1.06"]}});

    public static List getList(String line) {
        Pattern pattern = Pattern.compile("\\(([^)]+)\\)");
        Matcher matcher = pattern.matcher(line);

        Gson gson = new GsonBuilder()
                .enableComplexMapKeySerialization()
                .setDateFormat("yyyy-MM-dd HH:mm:ss")
                .create();

        if (matcher.find()) {
            String result = matcher.group(1); // 获取第一个捕获组内容

            LinkedTreeMap m = gson.fromJson(result, new TypeToken<LinkedTreeMap>() {}.getType());
            LinkedTreeMap data = (LinkedTreeMap) m.get("data");
            List klines = (List) data.get("klines");

            return klines;
        }
        return null;
    }

    public static List<Object> getData(String content) {
        List<String> lst = MaiDian.getList(content);
        if (lst != null) {
            List<Object> rs = new ArrayList<>();
            for (int j = lst.size() - 1; j > -1; j--) {
                String line = lst.get(j);
                List<Object> ls = new ArrayList<>();
                String[] ss = line.split(",");
                for (int i = 0; i < ss.length; i++) {
                    if (i == 0) {
                        ls.add(ss[i]);
                    } else {
                        ls.add(Float.valueOf(ss[i]));
                    }
                }
                rs.add(ls);
            }
            return rs;
        }
        return null;
    }

    public static List<Object> getData(List<String> lst) {
        //List<String> lst = MaiDian.getList(content);
        if (lst != null) {
            List<Object> rs = new ArrayList<>();
            for (int j = lst.size() - 1; j > -1; j--) {
                String line = lst.get(j);
                List<Object> ls = new ArrayList<>();
                String[] ss = line.split(",");
                for (int i = 0; i < ss.length; i++) {
                    if (i == 0) {
                        ls.add(ss[i]);
                    } else {
                        ls.add(Float.valueOf(ss[i]));
                    }
                }
                rs.add(ls);
            }
            return rs;
        }
        return null;
    }

    public static List<Integer> get_low(List<Object> lst) {
        List<Integer> rs = new ArrayList<>();
        for (int i = 0; i < lst.size(); i++) {
            if ((i - 5) >= 0 && (i + 5) < lst.size()) {
                List<Object> row = (List<Object>)lst.get(i);

                List<Object> row_1 = (List<Object>)lst.get(i - 1);
                List<Object> row_2 = (List<Object>)lst.get(i + 1);
                List<Object> row_11 = (List<Object>)lst.get(i - 2);
                List<Object> row_21 = (List<Object>)lst.get(i + 2);
                float price = (float)row.get(2);
                float price_1 = (float)row_1.get(2);
                float price_2 = (float)row_2.get(2);
                float price_11 = (float)row_11.get(2);
                float price_21 = (float)row_21.get(2);
                float v1 = get_average_1(lst, i);
                float v2 = get_average_2(lst, i);

                float v13 = get_average_13(lst, i);
                float v23 = get_average_23(lst, i);
                if (((price < price_1 && price > price_2) || (price < price_11 && price > price_21)) && price < v1 && (price < v2 || price_2 < v2) && price < v13 && (price < v23 || price_2 < v23)) {
                    rs.add(i);
                }
            }
        }
        return rs;
    }

    public static List<Integer> get_low_1(List<Object> lst) {
        List<Integer> rs = new ArrayList<>();
        for (int i = 0; i < lst.size(); i++) {
            if ((i - 5) >= 0 && (i + 5) < lst.size()) {
                List<Object> row = (List<Object>)lst.get(i);
                List<Object> row_1 = (List<Object>)lst.get(i - 1);
                List<Object> row_2 = (List<Object>)lst.get(i + 1);
                List<Object> row_11 = (List<Object>)lst.get(i - 2);
                List<Object> row_21 = (List<Object>)lst.get(i + 2);
                float price = (float)row.get(2);
                float price_1 = (float)row_1.get(2);
                float price_2 = (float)row_2.get(2);
                float price_11 = (float)row_11.get(2);
                float price_21 = (float)row_21.get(2);
                //float v1 = get_average_1(lst, i);
                //float v2 = get_average_2(lst, i);

                float v13 = get_average_13(lst, i);
                float v23 = get_average_23(lst, i);
                if (((price < price_1 && price > price_2) || (price < price_11 && price > price_21)) && price < v13 && (price < v23 || price_1 < v23)) {
                    rs.add(i);
                }
            }
        }
        return rs;
    }

    public static Float get_average_1(List<Object> lst, int index) {
        List<Object> row_1 = (List<Object>)lst.get(index - 1);
        List<Object> row_2 = (List<Object>)lst.get(index - 2);
        List<Object> row_3 = (List<Object>)lst.get(index - 3);
        List<Object> row_4 = (List<Object>)lst.get(index - 4);
        List<Object> row_5 = (List<Object>)lst.get(index - 5);

        float price_1 = (float)row_1.get(2);
        float price_2 = (float)row_2.get(2);
        float price_3 = (float)row_3.get(2);
        float price_4 = (float)row_4.get(2);
        float price_5 = (float)row_5.get(2);

        return (Float) ((price_1 + price_2 + price_3 + price_4 + price_5) / 5);
    }

    public static Float get_average_2(List<Object> lst, int index) {
        List<Object> row_1 = (List<Object>)lst.get(index + 1);
        List<Object> row_2 = (List<Object>)lst.get(index + 2);
        List<Object> row_3 = (List<Object>)lst.get(index + 3);
        List<Object> row_4 = (List<Object>)lst.get(index + 4);
        List<Object> row_5 = (List<Object>)lst.get(index + 5);

        float price_1 = (float)row_1.get(2);
        float price_2 = (float)row_2.get(2);
        float price_3 = (float)row_3.get(2);
        float price_4 = (float)row_4.get(2);
        float price_5 = (float)row_5.get(2);

        return (Float) ((price_1 + price_2 + price_3 + price_4 + price_5) / 5);
    }

    public static Float get_average_13(List<Object> lst, int index) {
        List<Object> row_1 = (List<Object>)lst.get(index - 1);
        List<Object> row_2 = (List<Object>)lst.get(index - 2);
        List<Object> row_3 = (List<Object>)lst.get(index - 3);

        float price_1 = (float)row_1.get(2);
        float price_2 = (float)row_2.get(2);
        float price_3 = (float)row_3.get(2);

        return (Float) ((price_1 + price_2 + price_3) / 3);
    }

    public static Float get_average_23(List<Object> lst, int index) {
        List<Object> row_1 = (List<Object>)lst.get(index + 1);
        List<Object> row_2 = (List<Object>)lst.get(index + 2);
        List<Object> row_3 = (List<Object>)lst.get(index + 3);

        float price_1 = (float)row_1.get(2);
        float price_2 = (float)row_2.get(2);
        float price_3 = (float)row_3.get(2);

        return (Float) ((price_1 + price_2 + price_3) / 3);
    }

    public static List<Float> getK(List<Object> lst, List<Integer> rs) {
        List<Float> lsrs = new ArrayList<>();
        for (int i = 0; i < rs.size(); i += 2) {
            if (i + 1 < rs.size()) {
                int first = rs.get(i);
                int end = rs.get(i + 1);

                List<Object> row = (List<Object>) lst.get(first);
                List<Object> row_1 = (List<Object>) lst.get(end);
                Float h = (Float) row.get(2) - (Float) row_1.get(2);
                float myrs = h / (end - first);
                lsrs.add(myrs);
            }
        }
        return lsrs;
    }

    public static Float get_price_x(List<Object> lst, List<Integer> rs, List<Float> lsrs) {
        Float k = lsrs.get(0);
        Integer index = rs.get(0);
        List<Object> end_row = (List<Object>)lst.get(index);
        Float price = (Float)end_row.get(2);
        return price + k * index;
    }
}
